#include<iostream>
#include<algorithm>
using namespace std;
struct task{
	int a;
	int b;
	int z;
	int bh;
};
bool comp(task x,task y){
	if(x.z==y.z && x.z==2){
		return x.b>y.b;
	}
	else if(x.z==y.z && x.z==1){
		return x.a<y.a;
	}
	else{
		return x.z<y.z;
	}
}
int main(){
	int n;
	cin>>n;
	task tasks[n];
	for(int i=0;i<n;i++){
		tasks[i].bh=i+1;
		cin>>tasks[i].a>>tasks[i].b;
		if(tasks[i].a<tasks[i].b){
			tasks[i].z=1;
		}
		else{
			tasks[i].z=2;
		}
	}
	sort(tasks,tasks+n,comp);
	int ij=0,counts=tasks[0].a,countx=tasks[0].a,zh=1;
	while(1){
		if(counts>countx){
			counts+=tasks[ij].b;
			zh=1;
		}
		else if(counts<=countx){
			countx+=tasks[ij].b;
			counts+=tasks[ij].a;
			zh=0; 
		}
		if(ij==(n-1)){
			break;
		}
		ij++;
	}
	if(zh==1){
		cout<<counts;
	}
	else{
		cout<<countx;
	}
	cout<<endl;
	for(int i=0;i<n-1;i++){
		cout<<tasks[i].bh<<" ";
	}
	cout<<tasks[n-1].bh;
} 
